use std::env;

use anyhow::Result;
use config::{Config, Environment, File};
use log::debug;
use serde::Deserialize;

mod app;
mod whisper;

use app::AppSettings;
use whisper::WhisperSettings;

#[derive(Debug, Deserialize)]
pub struct Settings {
    pub app: AppSettings,
    pub whisper: WhisperSettings,
}

impl Settings {
    pub fn new() -> Result<Self> {
        let run_mode = env::var("APP_RUN_MODE").unwrap_or("dev".into());
        let settings = Config::builder()
            .add_source(File::with_name(&format!("configs/{run_mode}.yaml")).required(false))
            .add_source(Environment::with_prefix("APP").separator("-"))
            .build()
            .map_err(anyhow::Error::from)?;

        settings
            .try_deserialize::<Self>()
            .map_err(Into::into)
            .map(|settings| {
                debug!("Settings: {settings:?}");
                settings
            })
    }
}
